package com.zzty.txj1.controller;


import com.zzty.txj1.entity.Question;
import com.zzty.txj1.entity.SelfExam;
import com.zzty.txj1.entity.SelfExamTransaction;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.QuestionService;
import com.zzty.txj1.service.SelfExamService;
import com.zzty.txj1.service.SelfExamTransactionService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/selfExamTransaction")
public class SelfExamTransactionController {

    @Autowired
    QuestionService questionService;
    @Autowired
    SelfExamTransactionService selfExamTransactionService;

    @Autowired
    private SelfExamService selfExamService;

    @RequestMapping(value = "/checkFirstLogIn")
    public SelfExamTransaction checkFirstLogIn(SelfExamTransaction selfExamTransaction, HttpServletRequest request){
        SelfExamTransaction selfExamTransaction1=new SelfExamTransaction();
        User user=(User)request.getSession().getAttribute("user");
        selfExamTransaction.setUserId(user.getId());
        selfExamTransaction1=selfExamTransactionService.checkFirstLogIn(selfExamTransaction);
        return selfExamTransaction1;
    }

    @RequestMapping("/initSelfExamTransaction")
    public Integer initSelfExamTransaction(SelfExamTransaction selfExamTransaction,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        selfExamTransaction.setUserId(user.getId());
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(d);
        selfExamTransaction.setStartTime(date);
        Integer flag=selfExamTransactionService.initSelfExamTransaction(selfExamTransaction);
        return flag;
    }

    @RequestMapping("/getStartTime")
    public String getStartTime(SelfExamTransaction selfExamTransaction,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        selfExamTransaction.setUserId(user.getId());
        String startTime=selfExamTransactionService.getStartTime(selfExamTransaction);
        return startTime;
    }

    @RequestMapping(value = "/updateUrlSpendTime",method = RequestMethod.POST)
    public Integer updateUrlSpendTime(SelfExamTransaction selfExamTransaction,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        String answerUrl=(String) request.getSession().getAttribute("answerUrl");
        selfExamTransaction.setUserId(user.getId());
        selfExamTransaction.setAnswerUrl(answerUrl);
        Integer flag=selfExamTransactionService.updateUrlSpendTime(selfExamTransaction);
        return flag;
    }
}
